package com.javapoint.view.managedbeans;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;

import com.javapoing.bo.UsuarioBO;
import com.javapoint.SpringContext;
import com.javapoint.ibatis.mappings.dao.UserDAO;

public class UserService implements Serializable {
	private static final long serialVersionUID = 3342887013219197470L;

	private String id;
	private String userName;
	private String password;
	private String firstName;
	private String lastName;
	private String emailAddress;
	private String positionAddress;
	private String userDetails;
	private Date lastModification;
	private Date accountAge;
	private Date dateOfBirth;
	private String socialSecurityNumber;
	private Date reportedDate;
	private String description;
	private String languagePreference;
	private String[] preferences;
	private List<String> country;
	private List<SelectItem> countryDataModel;
	private List<com.javapoint.mapping.entity.vo.User> users;
	private com.javapoint.mapping.entity.vo.User user;

	public com.javapoint.mapping.entity.vo.User getUser() {
		return user;
	}

	public void setUser(com.javapoint.mapping.entity.vo.User user) {
		this.user = user;
	}

	public List<com.javapoint.mapping.entity.vo.User> getUsers() {
		users = new ArrayList<com.javapoint.mapping.entity.vo.User>();
		for(int i=0;i < 30;i++){
			com.javapoint.mapping.entity.vo.User user = new com.javapoint.mapping.entity.vo.User();
			user.setId("userId_"+i);
			user.setFirstName("first_name_"+i);
			user.setLastName("last_Name_"+i);
			user.setEmailAddress("emailAddress_"+1+"@hotmail.com");
			user.setAccountAge(new Date(System.currentTimeMillis()-(i*1000)));
			users.add(user);
			}
		return users;
	}
	
	public String editUser(){
		FacesContext context = FacesContext.getCurrentInstance(); 
		Map map = context.getExternalContext().getRequestParameterMap();
		String userId = (String) map.get("id");
		List users = getUsers();
		Iterator i= users.iterator();
		boolean found=false;
		while(i.hasNext()){
			com.javapoint.mapping.entity.vo.User tmpUser = (com.javapoint.mapping.entity.vo.User)i.next();
			if(tmpUser.getId().equalsIgnoreCase(userId)){
				user = tmpUser;
				found=true;
			}
		}
		if(found){
			return "editUser";
		}else{
			return "errorMessage";
		}
	}

	public void setUsers(List<com.javapoint.mapping.entity.vo.User> users) {
		this.users = users;
	}

	public String[] getPreferences() {
		return preferences;
	}

	public void setPreferences(String[] preferences) {
		this.preferences = preferences;
	}

	public String getLanguagePreference() {
		return languagePreference;
	}

	public void setLanguagePreference(String languagePreference) {
		this.languagePreference = languagePreference;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getPositionAddress() {
		return positionAddress;
	}

	public void setPositionAddress(String positionAddress) {
		this.positionAddress = positionAddress;
	}

	public String getUserDetails() {
		return userDetails;
	}

	public void setUserDetails(String userDetails) {
		this.userDetails = userDetails;
	}

	public Date getLastModification() {
		return lastModification;
	}

	public void setLastModification(Date lastModification) {
		this.lastModification = lastModification;
	}

	public Date getAccountAge() {
		return accountAge;
	}

	public void setAccountAge(Date accountAge) {
		this.accountAge = accountAge;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getSocialSecurityNumber() {
		return socialSecurityNumber;
	}

	public void setSocialSecurityNumber(String socialSecurityNumber) {
		this.socialSecurityNumber = socialSecurityNumber;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getReportedDate() {
		return reportedDate;
	}

	public void setReportedDate(Date reportedDate) {
		this.reportedDate = reportedDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void changeToEnglish() {
		languagePreference = "en";
	}

	public void changeToSpanish() {
		languagePreference = "es";
	}

	public List<SelectItem> getCountryDataModel() {
		String[] countryList = { "Afghanistan                       ",
				"Albania                           ",
				"Algeria                           ",
				"Andorra                           ",
				"Angola                            ",
				"Antigua & Deps                    ",
				"Argentina                         ",
				"Armenia                           ",
				"Australia                         ",
				"Austria                           ",
				"Azerbaijan                        ",
				"Bahamas                           ",
				"Bahrain                           ",
				"Bangladesh                        ",
				"Barbados                          ",
				"Belarus                           ",
				"Belgium                           ",
				"Belize                            ",
				"Benin                             ",
				"Bhutan                            ",
				"Bolivia                           ",
				"Bosnia Herzegovina                ",
				"Botswana                          ",
				"Brazil                            ",
				"Brunei                            ",
				"Bulgaria                          ",
				"Burkina                           ",
				"Burundi                           ",
				"Cambodia                          ",
				"Cameroon                          ",
				"Canada                            ",
				"Cape Verde                        ",
				"Central African Rep               ",
				"Chad                              ",
				"Chile                             ",
				"China                             ",
				"Colombia                          ",
				"Comoros                           ",
				"Congo                             ",
				"Congo {Democratic Rep}            ",
				"Costa Rica                        ",
				"Croatia                           ",
				"Cuba                              ",
				"Cyprus                            ",
				"Czech Republic                    ",
				"Denmark                           ",
				"Djibouti                          ",
				"Dominica                          ",
				"Dominican Republic                ",
				"East Timor                        ",
				"Ecuador                           ",
				"Egypt                             ",
				"El Salvador                       ",
				"Equatorial Guinea                 ",
				"Eritrea                           ",
				"Estonia                           ",
				"Ethiopia                          ",
				"Fiji                              ",
				"Finland                           ",
				"France                            ",
				"Gabon                             ",
				"Gambia                            ",
				"Georgia                           ",
				"Germany                           ",
				"Ghana                             ",
				"Greece                            ",
				"Grenada                           ",
				"Guatemala                         ",
				"Guinea                            ",
				"Guinea-Bissau                     ",
				"Guyana                            ",
				"Haiti                             ",
				"Honduras                          ",
				"Hungary                           ",
				"Iceland                           ",
				"India                             ",
				"Indonesia                         ",
				"Iran                              ",
				"Iraq                              ",
				"Ireland {Republic}                ",
				"Israel                            ",
				"Italy                             ",
				"Ivory Coast                       ",
				"Jamaica                           ",
				"Japan                             ",
				"Jordan                            ",
				"Kazakhstan                        ",
				"Kenya                             ",
				"Kiribati                          ",
				"Korea North                       ",
				"Korea South                       ",
				"Kosovo                            ",
				"Kuwait                            ",
				"Kyrgyzstan                        ",
				"Laos                              ",
				"Latvia                            ",
				"Lebanon                           ",
				"Lesotho                           ",
				"Liberia                           ",
				"Libya                             ",
				"Liechtenstein                     ",
				"Lithuania                         ",
				"Luxembourg                        ",
				"Macedonia                         ",
				"Madagascar                        ",
				"Malawi                            ",
				"Malaysia                          ",
				"Maldives                          ",
				"Mali                              ",
				"Malta                             ",
				"Marshall Islands                  ",
				"Mauritania                        ",
				"Mauritius                         ",
				"Mexico                            ",
				"Micronesia                        ",
				"Moldova                           ",
				"Monaco                            ",
				"Mongolia                          ",
				"Montenegro                        ",
				"Morocco                           ",
				"Mozambique                        ",
				"Myanmar, {Burma}                  ",
				"Namibia                           ",
				"Nauru                             ",
				"Nepal                             ",
				"Netherlands                       ",
				"New Zealand                       ",
				"Nicaragua                         ",
				"Niger                             ",
				"Nigeria                           ",
				"Norway                            ",
				"Oman                              ",
				"Pakistan                          ",
				"Palau                             ",
				"Panama                            ",
				"Papua New Guinea                  ",
				"Paraguay                          ",
				"Peru                              ",
				"Philippines                       ",
				"Poland                            ",
				"Portugal                          ",
				"Qatar                             ",
				"Romania                           ",
				"Russian Federation                ",
				"Rwanda                            ",
				"St Kitts & Nevis                  ",
				"St Lucia                          ",
				"Saint Vincent & the Grenadines    ",
				"Samoa                             ",
				"San Marino                        ",
				"Sao Tome & Principe               ",
				"Saudi Arabia                      ",
				"Senegal                           ",
				"Serbia                            ",
				"Seychelles                        ",
				"Sierra Leone                      ",
				"Singapore                         ",
				"Slovakia                          ",
				"Slovenia                          ",
				"Solomon Islands                   ",
				"Somalia                           ",
				"South Africa                      ",
				"Spain                             ",
				"Sri Lanka                         ",
				"Sudan                             ",
				"Suriname                          ",
				"Swaziland                         ",
				"Sweden                            ",
				"Switzerland                       ",
				"Syria                             ",
				"Taiwan                            ",
				"Tajikistan                        ",
				"Tanzania                          ",
				"Thailand                          ",
				"Togo                              ",
				"Tonga                             ",
				"Trinidad & Tobago                 ",
				"Tunisia                           ",
				"Turkey                            ",
				"Turkmenistan                      ",
				"Tuvalu                            ",
				"Uganda                            ",
				"Ukraine                           ",
				"United Arab Emirates              ",
				"United Kingdom                    ",
				"United States                     ",
				"Uruguay                           ",
				"Uzbekistan                        ",
				"Vanuatu                           ",
				"Vatican City                      ",
				"Venezuela                         ",
				"Vietnam                           ",
				"Yemen                             ",
				"Zambia                            ",
				"Zimbabwe                          " };
		List<SelectItem> arrayListCount = new ArrayList<SelectItem>(countryList.length);
		for (String countryTmp : countryList) {
			SelectItem item = new SelectItem();
			item.setLabel(countryTmp);
			item.setValue(countryTmp);
			arrayListCount.add(item);
		}
		return arrayListCount;
	}

	public void setCountryDataModel(List<SelectItem> countryDataModel) {
		this.countryDataModel = countryDataModel;
	}

	public List<String> getCountry() {
		return country;
	}

	public void setCountry(List<String> country) {
		this.country = country;
	}
	
	public String validate(){
		try {
			UsuarioBO userbo = new UsuarioBO();
			userbo.regristrarUsuario(user);
			return "confirm";
		} catch (Exception e) {
			e.printStackTrace();
			return "errorPage";
		}
	}
	
	public String clean(){
		
		return "reload";
		
	}
}
